﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Threading;


namespace test
{
    public class Cell
    {
        
        public delegate void RILRESULTCALLBACK(uint dwCode, IntPtr hrCmdID, IntPtr lpData, uint cbData, uint dwParam);
        public delegate void RILNOTIFYCALLBACK(uint dwCode, IntPtr lpData, uint cbData, uint dwParam);
        public static IntPtr hRil;
        [StructLayout(LayoutKind.Explicit)]
        class RILCELLTOWERINFO
        {
            [FieldOffset(0)]
            uint dwSize;
            [FieldOffset(4)]
            uint dwParams;
            [FieldOffset(8)]
            public uint dwMobileCountryCode;
            [FieldOffset(12)]
            public uint dwMobileNetworkCode;
            [FieldOffset(16)]
            public uint dwLocationAreaCode;
            [FieldOffset(20)]
            public uint dwCellID;
            [FieldOffset(24)]
            uint dwBaseStationID;
            [FieldOffset(28)]
            uint dwBroadcastControlChannel;
            [FieldOffset(32)]
            uint dwRxLevel;
            [FieldOffset(36)]
            uint dwRxLevelFull;
            [FieldOffset(40)]
            uint dwRxLevelSub;
            [FieldOffset(44)]
            uint dwRxQuality;
            [FieldOffset(48)]
            uint dwRxQualityFull;
            [FieldOffset(52)]
            uint dwRxQualitySub;

        }

        [DllImport("ril.dll")]
        private static extern IntPtr RIL_Initialize(uint dwIndex, RILRESULTCALLBACK pfnResult, RILNOTIFYCALLBACK pfnNotify, uint dwNotificationClasses, uint dwParam, out IntPtr lphRil);
        [DllImport("ril.dll", EntryPoint = "RIL_GetCellTowerInfo")]
        private static extern IntPtr RIL_GetCellTowerInfo(IntPtr hRil);
        [DllImport("ril.dll", EntryPoint = "RIL_Hangup")]
        private static extern IntPtr RIL_Hangup(IntPtr hRil);
        [DllImport("ril.dll")]
        private static extern IntPtr RIL_Deinitialize(IntPtr hRil);

        private static RILCELLTOWERINFO _towerDetails;
        private static AutoResetEvent waithandle = new AutoResetEvent(false);
        private System.Windows.Forms.Timer _timer = new System.Windows.Forms.Timer();
        private CellData _currentTower;

        public Cell()
        {
            _timer.Interval = 5000; // 5 seconds
            _timer.Tick += new EventHandler(_timer_Tick);
        }
       
   
       
        private void _timer_Tick(object sender, EventArgs e)
        {
            
            _currentTower = GetCellTowerInfo();
        }


        public static CellData GetCellTowerInfo()
        {
            IntPtr radioInterfaceLayerHandle = IntPtr.Zero;
            IntPtr radioResponseHandle = IntPtr.Zero;

           
            radioResponseHandle = RIL_Initialize(1, new RILRESULTCALLBACK(CellDataCallback), null, 0, 0, out radioInterfaceLayerHandle);

            
            if (radioResponseHandle != IntPtr.Zero)
            {
                return null;
            }

           
            radioResponseHandle = RIL_GetCellTowerInfo(radioInterfaceLayerHandle);

          
            waithandle.WaitOne();

           
            RIL_Deinitialize(radioInterfaceLayerHandle);

           
            return new CellData()
            {
                Id = Convert.ToInt32(_towerDetails.dwCellID),
                Lac = Convert.ToInt32(_towerDetails.dwLocationAreaCode),
                Mcc = Convert.ToInt32(_towerDetails.dwMobileCountryCode),
                Mnc = Convert.ToInt32(_towerDetails.dwMobileNetworkCode),
            };
        }
        public static void CellDataCallback(uint dwCode, IntPtr hrCmdID, IntPtr lpData, uint cbData, uint dwParam)
        {
            
            _towerDetails = new RILCELLTOWERINFO();

            
            Marshal.PtrToStructure(lpData, _towerDetails);
      
           
            waithandle.Set();
        }




        #region ICellOriginator Members

        public CellData GetData()
        {
            return GetCellTowerInfo();
        }

        #endregion

        #region IPositionOriginator Members

        public bool GetRadioStatus()
        {
            return _timer.Enabled;
        }

        public int SetRadioOn()
        {
            try
            {
                _timer.Enabled = true;
            }
            catch
            {
                return 1;
            }
            return 0;
        }

        public int SetRadioOff()
        {
            try
            {
                _timer.Enabled = false;
            }
            catch
            {
                return 1;
            }
            return 0;
        }

        public int SetRadioOn(int interval)
        {
            throw new NotImplementedException();
        }

        public int SetRadioOff(int interval)
        {
            throw new NotImplementedException();
        }

        public int DataRequest()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
